package com.zyh.flink.day03;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class ValueStateTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> hadoop10 = environment.socketTextStream("hadoop10", 9999);
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = hadoop10.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] words = s.split("\\s+");
                for (String word : words) {
                    collector.collect(word);
                }
            }
        }).map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                return Tuple2.of(s,1);
            }
        }).keyBy(t -> t.f0);

        SingleOutputStreamOperator<Tuple2<String, Integer>> richMapStream = keyedStream.map(new MyRichMapFunction());
        richMapStream.print();

        environment.execute("ValueStateJob");
    }
}
//在继承RichMapFunction的时候，需要设置两个类型参数
//IN:表示输入数据类型，上游流中的数据类型
//OUT:表示输出数据类型，根据需要写
//class MyMapFunction extends RichMapFunction[IN,OUT]

class MyRichMapFunction extends RichMapFunction<Tuple2<String,Integer>,Tuple2<String,Integer>>{
    //    通过valueState存储单词的个数，因此valueState里面存储的数据类型是Integer
    private ValueState<Integer> valueState;

    @Override
    public void open(Configuration parameters) throws Exception {
        //1.获取到RuntimeContext对象
        RuntimeContext context = getRuntimeContext();

        //2.创建状态描述符：状态名和状态中存储的数据类型信息
        ValueStateDescriptor<Integer> vsd = new ValueStateDescriptor<>("vsd", Types.INT);

        //3.根据状态描述符创建状态
        valueState = context.getState(vsd);
    }

    /**
     * map方法，是每传输过来一个数据就会执行一次的
     *
     * @param value 上游发送过来的数据，(单词,1)
     * @return 返回给下游的数据
     */
    @Override
    public Tuple2<String, Integer> map(Tuple2<String, Integer> value) throws Exception {
        //从状态中把单词的个数获取到
        //然后加1，得到一个新结果
        //把这个新结果重新放入到状态中，把原来的数据替换掉

        //1.从状态中获取数据
        Integer oldCount = valueState.value();
        //2.计算
        Integer newCount = (oldCount == null ? 0:oldCount) + 1;
        //3.把计算结果写入到状态，就会把状态的原来的数据覆盖掉
        valueState.update(newCount);

        //构建返回值
        return Tuple2.of(value.f0,newCount);
    }
}